$(function() {
	var nowdate = new Date().Format("yyyy-MM-dd");
	$('#search_end').val(nowdate);

	var start = addDate(nowdate, -30);
	$('#search_start').val(start);
	
	doQuery();
});

function doQuery() {
	//console.log('do query');

	var start = $("#search_start").val();
	var end = $("#search_end").val();

	$.ajax({
		type : "POST",
		url : "agentctrl",
		async : false,
		data : {
			"start" : start,
			"end" : end
		},
		dataType : "text",
		success : function(msg) {
			var info = JSON.parse(msg);
			// console.log(info);

			var objTable = $("#tablepanel");
			var tr = '';

			var categories = [];
			var datas = [];

			for ( var key in info) {
				var obj = info[key];

				tr += '<tr>';
				tr += '<td>';
				tr += key;
				tr += '</td>';

				tr += '<td>';
				tr += obj;
				tr += '</td>';
				tr += '</tr>';

				categories.push(key);
				datas.push({
					name : key,
					value : obj
				});
			}

			$("#tablepanel  tr:not(:first)").html("");
			objTable.append(tr);

			// console.log(myChart);
			myChart.setOption({
				xAxis : {
					axisLabel : {
						interval : 0,
						rotate : 40
					},
					data : categories
				},
				yAxis : {},
				series : [ {
					name : '调班次数',
					data : datas
				} ]
			});

		},
		error : function(data) {
			console.log('error');
		}
	});

}

Date.prototype.Format = function(fmt) {
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
					: (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

function addDate(date, days) {
	var d = new Date(date);
	d.setDate(d.getDate() + days);

	var month = d.getMonth() + 1;
	var day = d.getDate();

	if (month < 10) {
		month = "0" + month;
	}
	if (day < 10) {
		day = "0" + day;
	}

	return d.getFullYear() + '-' + month + '-' + day;
}